package com.hengfei.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hengfei.dao.TCheckgroupMapper;
import com.hengfei.dao.TCheckitemMapper;
import com.hengfei.dao.TSetmealCheckgroupMapper;
import com.hengfei.dao.TSetmealMapper;
import com.hengfei.entity.PageResult;
import com.hengfei.entity.QueryPageBean;
import com.hengfei.pojo.*;
import com.hengfei.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {
    //TSetmeal表
    @Autowired
    private TSetmealMapper tSetmealMapper;

    //tCheckgroup
    @Autowired
    private TCheckgroupMapper tCheckgroupMapper;

    //tCheckItem
    @Autowired
    private TCheckitemMapper tCheckitemMapper;

    //套餐表和检查组表的中间表
    @Autowired
    private TSetmealCheckgroupMapper tSetmealCheckgroupMapper;

    //分页查询，返回数据
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //开始分页，两个参数：        当前页码，每页数据条数
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());

        String queryString = queryPageBean.getQueryString();

        TSetmealExample example = null;
        if(queryString != null && queryString.length() != 0) {
            //此处是条件查询代码
            example = new TSetmealExample();
            String vagueQueryStr = "%" + queryString + "%";
            example.or().andNameLike(vagueQueryStr);
            example.or().andHelpcodeLike(vagueQueryStr);
            example.or().andCodeLike(vagueQueryStr);
            example.or().andRemarkLike(vagueQueryStr);
        }
        Page<TSetmeal> page = (Page<TSetmeal>) tSetmealMapper.selectByExample(example);

        return new PageResult(page.getTotal(), page.getResult());
    }

    //添加套餐数据。并且在中间表插入 checkgroupIds 和当前 setmeal.getId() 绑定
    @Override
    public void add(TSetmeal setmeal, Integer[] checkgroupIds) {
        //先添加setmeal表的信息
        //此处添加，在xml配置文件的SQL语句中作了相应操作，以将新插入数据的id，存入到 setmeal.id 中
        tSetmealMapper.insertSelective(setmeal);
        //再添加中间表信息
        Integer setmealId = setmeal.getId();
        //插入中间表信息
        for (Integer checkgroupId : checkgroupIds) {
            TSetmealCheckgroupKey tSetmealCheckgroupKey = new TSetmealCheckgroupKey(setmealId, checkgroupId);
            tSetmealCheckgroupMapper.insertSelective(tSetmealCheckgroupKey);
        }
    }

    @Override
    public List<TSetmeal> findAll() {
        return tSetmealMapper.selectByExample(null);
    }

    //根据id查找setmeal信息，setmeal对象中要包含其内含的 checkgroup 信息，每一个checkgroup中又包含其内含的checkitem信息
    @Override
    public TSetmeal findById(int id) {
        //先根据id查找对应的setmeal信息
        TSetmeal tSetmeal = tSetmealMapper.selectByPrimaryKey(id);

        //根据setmeal 的 id，连接查询 t_checkgroup 和 t_setmeal_checkgroup 表，查询出该setmeal对应的所有 checkgroup
        ArrayList<TCheckgroup> tCheckgroups = tCheckgroupMapper.selectCheckgroupsBySetmealId(id);
        tSetmeal.setCheckGroups(tCheckgroups);

        for (TCheckgroup tCheckgroup : tCheckgroups) {
            //根据checkgroup的id查询对应的checkitem的信息
            ArrayList<TCheckitem> tCheckitems = tCheckitemMapper.selectCheckitemsByCheckgroupId(tCheckgroup.getId());
            tCheckgroup.setCheckItems(tCheckitems);
        }

        return tSetmeal;
    }


}
